//
//  SchemeHandler.m
//  MasterDuoBao
//
//  Created by 汤丹峰 on 16/3/23.
//  Copyright © 2016年 wenzhan. All rights reserved.
//

#import "SchemeHandler.h"
#import "SchemeDefine.h"
#import "VCManager.h"
#import "MDUrl.h"
#import "UtilTool.h"
#import "BaseWebViewController.h"

@interface SchemeHandler ()

@end

@implementation SchemeHandler

static SchemeHandler *m_handler;
+ (id)defaultHandler{
    static dispatch_once_t pred;
    dispatch_once(&pred, ^{
        m_handler = [[SchemeHandler alloc] init];
    });
    return m_handler;
}

#pragma mark --- public method
-(void)handleUrl:(NSString *)urlStr animated:(BOOL)animated{
    //先判断是否是以我们定义的scheme开始
    if ([SchemeHandler isLocalScheme:[NSURL URLWithString:urlStr]]){
        //根据url,获取path类型:处理web和native
        HandleType type = [self getHandleType:urlStr];
        switch (type){
            case TYPE_OPEN_WEB:
                [self handleOpenWeb:urlStr animated:animated];
                break;
            case TYPE_OPEN_NATIVE:
                [self handleOpenNative:urlStr animated:animated];
                break;
            case TYPE_OPEN_SHARE:
                [self handleOpenShare:urlStr animated:animated];
                break;
            case TYPE_OPEN_CLOSE:
                break;
            case TYPE_INVALID:
                return;
        }
    }else{
        NSURL *url = [self checkURLForString:urlStr];
        if (url) {
            // 处理WEB
            [self handleOpenWeb:[NSString stringWithFormat:@"%@",url] animated:animated];
        }else{}
    }

}


#pragma mark ---- 处理  WEB  协议
- (void)handleOpenWeb:(NSString*)urlStr animated:(BOOL)animated{
    BaseWebViewController *webcontroller = [[BaseWebViewController alloc]init];
    webcontroller.urlString = urlStr;
    webcontroller.webTitle = @"活动";
    UIViewController *topcontroller = [[VCManager shareVCManager]getTopViewController];
    [topcontroller.navigationController pushViewController:webcontroller animated:animated];
}

#pragma mark ---- 处理  Native  协议
- (void)handleOpenNative:(NSString*)urlStr animated:(BOOL)animated{
    NSURL *url = [NSURL URLWithString:urlStr];
    if ([url.host isEqualToString:URL_HOST]){
        NSString *value = [url getValueInQueryForKey:URL_QUERY_KEY_VCNAME isCaseSensitive:NO];
        if (value) {

        }else{
            [MBProgressHUD showTipMessageInWindow:@"链接中没有传入控制器的名称"];
        }
    }else{
        [MBProgressHUD showTipMessageInWindow:@"链接中的域名不正确"];
    }
}

#pragma mark ---- 处理  Share  协议
- (void)handleOpenShare:(NSString*)urlStr animated:(BOOL)animated{
    
}


#pragma mark --- private method
// 判断当前链接是否是自定义url
+(BOOL)isLocalScheme:(NSURL *)urlStr{
    if ([urlStr.scheme isEqualToString:URL_SCHEME]) {
        return YES;
    }
    return NO;
}
// 判断当前链接是否是http或者https
- (NSURL *)checkURLForString:(NSString *)str {
    NSURL *     result;
    NSString *  trimmedStr;
    NSRange     schemeMarkerRange;
    NSString *  scheme;
    assert(str != nil);
    result = nil;
    trimmedStr = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if ( (trimmedStr != nil) && (trimmedStr.length != 0) ) {
        schemeMarkerRange = [trimmedStr rangeOfString:@"://"];
        if (schemeMarkerRange.location == NSNotFound) {
            result = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@", trimmedStr]];
        } else {
            scheme = [trimmedStr substringWithRange:NSMakeRange(0, schemeMarkerRange.location)];
            assert(scheme != nil);
            if ( ([scheme compare:@"http"  options:NSCaseInsensitiveSearch] == NSOrderedSame)
                || ([scheme compare:@"https" options:NSCaseInsensitiveSearch] == NSOrderedSame) ) {
                result = [NSURL URLWithString:trimmedStr];
            } else {
                // 格式不符合
            }
        }
    }
    return result;
}


//通过path 获取当前操作类型
- (HandleType)getHandleType:(NSString*)urlStr{
    NSURL *url = [NSURL URLWithString:urlStr];
    //先判断域名是否是我们定义的域名
    if ([url.host isEqualToString:URL_HOST]) {
        NSString *path = url.path;
        //再判断路径
        if ([path isEqualToString:URL_PATH_WEB]){
            // /jump
            return TYPE_OPEN_WEB;
        }else if ([path isEqualToString:URL_PATH_NATIVE]){
            // /native
            return TYPE_OPEN_NATIVE;
        }else if ([path isEqualToString:URL_PATH_SHARE]){
            // /share
            return TYPE_OPEN_SHARE;
        }else if ([path isEqualToString:URL_PATH_CLOSR]){
            // /close
            return TYPE_OPEN_CLOSE;
        }
    }
    return TYPE_INVALID;
}




@end







